package Finance::QuoteHist::cnYES;

use strict;
use vars qw(@ISA $VERSION);
use Carp;

$VERSION = '1.00';

use Finance::QuoteHist::Generic;
@ISA = qw(Finance::QuoteHist::Generic);

use Date::Manip;
Date::Manip::Date_Init("TZ=GMT");

# Example for HTML output:
#
# http://cnyes.megatime.com.tw/asp/stockinfo/ps_historyprice.asp?start=2000/1&end=2009/10&stockid=2330
#

sub new {
  my $that = shift;
  my $class = ref($that) || $that;
  my %parms = @_;
  my $self = __PACKAGE__->SUPER::new(%parms);
  bless $self, $class;
  $self->parse_mode('html');
  $self;
}

sub url_maker {
  my($self, %parms) = @_;
  my $target_mode = $parms{target_mode} || $self->target_mode;
  my $parse_mode  = $parms{parse_mode}  || $self->parse_mode;
  # *always* block unknown target/mode cominations

  return undef unless $target_mode eq 'quote' && $parse_mode eq 'html';
  my($ticker, $start_date, $end_date) = @parms{qw(symbol start_date end_date)};
  $start_date ||= $self->start_date;
  $end_date   ||= $self->end_date;

  my $host = 'cnyes.megatime.com.tw';
  my $uri  = 'asp/stockinfo/ps_historyprice.asp';

  my($sy, $sm, $sd) = $self->ymd($start_date);
  my($ey, $em, $ed) = $self->ymd($end_date);
  my $base_url = "http://$host/$uri?";
  my @base_parms = (
    "start=$sy/$sm",
    "end=$ey/$em",
    "stockid=$ticker",
  );
  my @urls =  ($base_url .  join('&', @base_parms));
  sub { pop @urls };
}

sub html_parser {
  my $self = shift;
  sub {
    my $data = shift;
    my $html_string;
    if (ref $data) {
      local($/);
      $html_string = <$data>;
    }
    else {
      $html_string = $data;
    }
    my $rows;
    while($html_string =~ m{<tr[^>]*>(.*?)</tr>}i)
    {
      my $tr = $1;
      $html_string = $';
      next if $tr !~ m{b(s|w)r9};
      my @TD = ($tr =~ m{<td[^>]*>\s*(.*?)\s*</td>}ig);
      my ($date, $open, $high, $low, $close, $vol) = @TD;
      push(@$rows, [$date, $open, $high, $low, $close, $vol]);
    }
    return $rows;
  }
}

1;

__END__

=head1 NAME

Finance::QuoteHist::cnYES - Site-specific subclass for retrieving historical stock quotes.

=head1 SYNOPSIS

  use Finance::QuoteHist::cnYES;
  $q = new Finance::QuoteHist::cnYES
     (
      symbols    => [qw(2330)],
      start_date => '2009/10/01',
      end_date   => 'today',
     );

  # Values
  foreach $row ($q->quotes()) {
    ($symbol, $date, $open, $high, $low, $close, $volume) = @$row;
    ...
  }

=head1 DESCRIPTION

Finance::QuoteHist::cnYES is a subclass of
Finance::QuoteHist::Generic, specifically tailored to read historical
quotes from the cnYES web site
(I<http://www.cnyes.com/>) for Taiwan market.

MSN does not currently provide information on dividends or splits.

Please see L<Finance::QuoteHist::Generic(3)> for more details on usage
and available methods. If you just want to get historical quotes and
are not interested in the details of how it is done, check out
L<Finance::QuoteHist(3)>.

=head1 METHODS

The basic user interface consists of three methods, as seen in the
example above. Those methods are:

=over

=item quotes()

Returns a list of rows (or a reference to an array containing those
rows, if in scalar context). Each row contains the B<Symbol>, B<Date>,
B<Open>, B<High>, B<Low>, B<Close>, and B<Volume> for that date.

=back

The following methods override methods provided by the
Finance::QuoteHist::Generic module; more of this was necessary than is
typical for a basic query site due to the variety of query types and
data formats available on cnYES.

=over

=item url_maker()

Returns a subroutine reference tailored for the current target mode and
parsing mode. The routine is an iterator that will produce all necessary
URLs on repeated invocations necessary to complete a query.

=back

=head1 REQUIRES

Finance::QuoteHist::Generic

=head1 DISCLAIMER

The data returned from these modules is in no way guaranteed, nor are
the developers responsible in any way for how this data (or lack
thereof) is used. The interface is based on URLs and page layouts that
might change at any time. Even though these modules are designed to be
adaptive under these circumstances, they will at some point probably
be unable to retrieve data unless fixed or provided with new
parameters. Furthermore, the data from these web sites is usually not
even guaranteed by the web sites themselves, and oftentimes is
acquired elsewhere.

If you would like to know more, check out the terms of service from
cnYES, which can be found here (Traditional Chinese, Big5):

  http://www.cnyes.com/asp/announce_service.asp

If you still have concerns, then use another site-specific historical
quote instance, or none at all.

Above all, play nice.

=head1 AUTHOR

Yen-Ming Lee E<lt>F<leeym@leeym.com>E<gt>

=head1 COPYRIGHT

Copyright (c) 2009 Yen-Ming Lee. All rights reserved.
This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

Finance::QuoteHist::Generic(3), Finance::QuoteHist(3), perl(1).

=cut
